/**AI基类 */
class AIBase {
    /**击球花色 */
    private myColor: number;
    /**状态
     * 0-空挡
     * 1-摆球
     * 2-自由球
     * 3-击球
     */
    private type: number;
    /**方向速度 */
    private Vspeed;
    /**屏幕移动x坐标 */
    private stageX: number;
    /**屏幕移动y坐标 */
    private stageY: number;
    /**目标点x */
    private targeX: number;
    /**目标点Y */
    private targeY: number;
    /**杆的方向 */
    private ganPoint: egret.Point;
    constructor() {
        this.type = 0;
        this.myColor = -1;
        this.Vspeed = {
            vx: 0,
            vy: 0
        }
        this.stageX = 0;
        this.stageY = 0;
    }
    /**状态选择 */
    public changeType(type) {
        this.type = type;
    }
    /**更新 */
    public update() {
        if (this.type != 0) {
            switch (this.type) {
                case 1:
                    egret.setTimeout(this.putBall, this, Math.random() * 1000 + 1000);
                    break;
                case 2:
                    egret.setTimeout(this.freeBall, this, Math.random() * 1000 + 1000);
                    break;
                case 3:
                    egret.setTimeout(this.getMoveGanData, this, Math.random() * 1000 + 1000);
                    break;
            }
            this.type = 0;
        }
    }
    /**击球 */
    public shoot() {
        FakeServerManage.getInstance().Shoot(Math.floor(Math.random() * 125 + 125), this.ganPoint);
    }
    /**重置AI */
    public restAI() {
        this.type = 0;
        this.myColor = -1;
        this.Vspeed = {
            vx: 0,
            vy: 0
        }
        this.stageX = 0;
        this.stageY = 0;
    }
    /**摆球 */
    public putBall() {
        //摆球范围（x 200-400      y 200-600）
        var putBallDate = {
            x: Math.floor(Math.random() * 200 + 200),
            y: Math.floor(Math.random() * 200 + 400),
            type: 1
        }
        FakeServerManage.getInstance().puBall(putBallDate);
        egret.setTimeout(this.getMoveGanData, this, Math.random() * 1000 + 1000);
    }
    /**获取摆球坐标 */
    public getPutBallPoint() {
        /**我的位置 */
        let myPoint: egret.Point = new egret.Point(Math.floor(Math.random() * 200 + 850), Math.floor(Math.random() * 200 + 400));
        /**对方位置 */
        let youPoint: egret.Point = new egret.Point(0, 0);
        /**球的列表 */
        let ballArr = TableUI.Instance().ballArr;
        /**球的半径 */
        let ballRadius = ballArr[0].radius;
        for (let i = 0; i < ballArr.length; i++) {
            youPoint.setTo(ballArr[i].nextX, ballArr[i].nextY);
            /**距离 */
            let distance = egret.Point.distance(myPoint, youPoint);
            if (distance < ballRadius*2) {
                myPoint.setTo(Math.floor(Math.random() * 200 + 850), Math.floor(Math.random() * 200 + 400));
                i = -1;
            }
        }
        return myPoint;

    }
    /**自由球 */
    public freeBall() {
        //自由球范围（x 200-1050      y 200-600）
        let putBallPoint: egret.Point = this.getPutBallPoint();
        var putBallDate = {
            x: putBallPoint.x,
            y: putBallPoint.y,
            type: 2
        }
        FakeServerManage.getInstance().puBall(putBallDate);
        egret.setTimeout(this.getMoveGanData, this, Math.random() * 1000 + 1000);

    }
    /**杆的移动 */
    public moveGan() {
        var moveData = {
            stageX: this.stageX,
            stageY: this.stageY
        }
        FakeServerManage.getInstance().moveGan(moveData);
    }
    /**获取杆移动的信息 */
    public getMoveGanData() {
        let curBall = TableUI.Instance().curBall;
        let ballArr = TableUI.Instance().ballArr;
        /**最近的球 */
        let latelyBallPoint: egret.Point = this.getLatelyBallPoint();
        this.targeX = latelyBallPoint.x;
        this.targeY = latelyBallPoint.y;
        /**白球反方向 */
        let baiNegativeDirection: egret.Point = new egret.Point(curBall.nextX, curBall.nextY).subtract(latelyBallPoint);
        baiNegativeDirection.normalize(100);
        this.ganPoint = baiNegativeDirection;
        /**反方向坐标 */
        let negativeDirectionPoint: egret.Point = new egret.Point(curBall.nextX, curBall.nextY).add(baiNegativeDirection);
        this.stageX = negativeDirectionPoint.x;
        this.stageY = negativeDirectionPoint.y;
        Tween.get(this, { onChange: this.moveGan, onChangeObj: this }).to({ factor: 1, factor1: 1 }, Math.random() * 1000 + 3000, egret.Ease.backOut).call(this.shoot);

    }
    public get factor(): number {
        return 0;
    }
    public set factor(value: number) {
        this.stageX = (1 - value) * (1 - value) * this.stageX + 2 * value * (1 - value) * this.targeY + value * value * this.targeX;
    }
    public get factor1(): number {
        return 0;
    }
    public set factor1(value: number) {
        this.stageY = (1 - value) * (1 - value) * this.stageY + 2 * value * (1 - value) * this.targeX + value * value * this.targeY;
    }
    /**获取最近的球 */
    public getLatelyBallPoint() {
        let curBall = TableUI.Instance().curBall;
        let ballArr = TableUI.Instance().ballArr;
        /**距离 */
        let distance: number = 10000;
        /**最近的球 */
        let latelyBall: NewBall;
        for (let i = 0; i < ballArr.length; i++) {
            if (ballArr[i].idx == 0 || ballArr[i].isJindong == -2) {
                continue;
            }
            if (ballArr.length > 1) {
                if (this.myColor != -1) {
                    if (this.myColor != TableUI.Instance().seeAboutBallColor(ballArr[i].idx)) {
                        if (TableUI.Instance().seeAboutSurplus(this.myColor, ballArr)) {
                            continue;
                        }
                    }
                }
            }
            /**白球到彩球的距离 */
            let baiToBallDiantance = egret.Point.distance(new egret.Point(curBall.nextX, curBall.nextY), new egret.Point(ballArr[i].nextX, ballArr[i].nextY));
            if (baiToBallDiantance < distance) {
                distance = baiToBallDiantance;
                latelyBall = ballArr[i];
            }
        }
        return new egret.Point(latelyBall.nextX, latelyBall.nextY);

    }
    /**设置颜色 */
    public setColor(color) {
        this.myColor = color;
    }
    /**获取颜色 */
    public getColor() {
        return this.myColor;
    }

}